﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Back.Xinkui.Common.Control.Region;
using CommonFramework.Utility;
using Back.Xinkui.DataAccess.Buylead;
using Back.Xinkui.WebModel;
using Back.Xinkui.Common.Enum;
using Back.Xinkui.Entity.Buylead;

public partial class buylead_detail : PageAspxBase
{

    protected int BuyleadId
    {
        get
        {
            return WebHelper.GetQueryStringValue<int>("id", 0);
        }
    }

    protected int AuditStatus { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCountry();
            if (BuyleadId > 0)
            {
                BindBuylead();
            }
        }
    }
    /// <summary>
    /// 获取国家
    /// </summary>
    private void BindCountry()
    {
        this.ddlCountry.Items.Add(new ListItem("--Please select country / region-- ", "0"));
        List<CountryEntity> country = RegionManager.GetCountry();
        if (country != null && country.Count > 0)
        {
            foreach (CountryEntity conEntity in country)
            {
                this.ddlCountry.Items.Add(new ListItem(conEntity.Name, conEntity.CountryID.ToString()));
            }
        }
        this.ddlCountry.DataBind();
    }

    private void BindBuylead()
    {
        var buyleadEntity = BuyleadDataAccess.GetBuyleadById(BuyleadId);
        if (buyleadEntity != null)
        {
            this.txtSubject.Text = buyleadEntity.Subject;
            this.txtFirstName.Text = buyleadEntity.FirstName;
            this.txtLastName.Text = buyleadEntity.LastName;
            this.txtEmail.Text = buyleadEntity.Email;
            UC_Category1.CategoryID = buyleadEntity.CategoryID;
            this.txtDescription.Text = buyleadEntity.Details;
            if (buyleadEntity.Country > 0)
            {
                this.ddlCountry.SelectedValue = buyleadEntity.Country.ToString();
            }
            this.txtOem.Text = buyleadEntity.OEMNO;
            this.txtMainKeyWord.Text = buyleadEntity.MainKeyword;
            this.txtKeyWord.Text = buyleadEntity.Keywords;
            this.txtPacking.Text = buyleadEntity.Packing;
            this.txtQuantity.Text = buyleadEntity.Quantity;
            this.txtPriceRange.Text = buyleadEntity.PriceRange;
            if (buyleadEntity.PurchaseType == 0)
            {
                this.radioOrdinary.Checked = true;
            }
            this.hidLogo.Value = buyleadEntity.PicImgs;
            if (!string.IsNullOrEmpty(buyleadEntity.viewImage))
            {
                this.viewImg.ImageUrl = buyleadEntity.viewImage;
            }
            AuditStatus = (int)buyleadEntity.AuditStatus;
        }
    }
    
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        BuyleadDataAccess.UpdateBuyleadStatus(BuyleadId, (int)EAuditStatus.Approve, this.UserTicket.UserID);
        ShowAndReloadOpner("审核通过");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //获取表单数据
        BuyleadListEntity buylead = GetPostBuyleadInfo();

        if (BuyleadId > 0)
        {
            buylead.BuyleadID = BuyleadId;
            buylead.AdminID = this.UserTicket.UserID;
            BuyleadDataAccess.SaveBuyleadInfo(buylead);
            ShowAndReloadOpner("保存成功");
        }
        else
        {
            //新增产品
            //buylead.AdminID = this.UserTicket.UserID;
            //BuyleadDataAccess.CreateBuyleadInfo(buylead);
            ShowAndReloadOpner("操作失败");
        }
    }
    protected void btnReject_Click(object sender, EventArgs e)
    {
        BuyleadDataAccess.UpdateBuyleadStatus(BuyleadId, (int)EAuditStatus.Reject, this.UserTicket.UserID);
        ShowAndReloadOpner("操作成功");
    }
    /// <summary>
    /// 获取表单信息
    /// </summary>
    /// <returns></returns>
    private BuyleadListEntity GetPostBuyleadInfo()
    {
        BuyleadListEntity entity = BuyleadDataAccess.GetBuyleadById(BuyleadId);
        if (entity == null)
        {
            entity = new BuyleadListEntity();
        }
        entity.Subject = this.txtSubject.Text.Trim();
        entity.CategoryID = WebHelper.GetFormHtmlValue<int>("hidCaegory", 0);
        entity.Details = this.txtDescription.Text.Trim();
        entity.Country = Convert.ToInt32(this.ddlCountry.SelectedValue);
        entity.OEMNO = this.txtOem.Text.Trim();
        entity.MainKeyword = this.txtMainKeyWord.Text.Trim();
        entity.Keywords = this.txtKeyWord.Text.Trim();
        entity.Packing = this.txtPacking.Text.Trim();
        entity.Quantity = this.txtQuantity.Text.Trim();
        entity.PriceRange = this.txtPriceRange.Text.Trim();
        entity.FirstName = this.txtFirstName.Text.Trim();
        entity.LastName = this.txtLastName.Text.Trim();
        entity.Email = this.txtEmail.Text.Trim();
        if (this.radioOrdinary.Checked)
        {
            entity.PurchaseType = 0;
        }
        entity.PicImgs = this.hidLogo.Value;
        return entity;
    }
}